package beans.thread.executor;

import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ExecutorServiceDemo
{
	public static void main(String[] args)
	{
		Future<String> future = authenticate("");
		try
		{
			String lever = future.get().toString();
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
		catch (ExecutionException e)
		{
			e.printStackTrace();
		}
	}

	public static Future<String> authenticate(String msg)
	{
		ExecutorService pool = Executors.newFixedThreadPool(100);
		Socket client = new Socket();
		SocketAddress endpoint = new InetSocketAddress("ip", 9999);

		OutputStream outputStream = null;
		try
		{
			client.connect(endpoint, 100);
			outputStream = client.getOutputStream();
		}
		catch (Exception e)
		{
		}
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
		try
		{
			bw.write(msg);
			bw.flush();
		}
		catch (Exception e)
		{
		}

		Vip vip = new Vip("2222");
		Future<String> future = pool.submit(vip);

		return future;
	}
}
